package io.renren.modules.edu.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.JacksonUtils;
import io.renren.modules.edu.dao.TEduTrainTeacherDao;
import io.renren.modules.edu.dto.TEduTrainTeacherDTO;
import io.renren.modules.edu.entity.TEduTrainTeacherEntity;
import io.renren.modules.edu.service.TEduTrainTeacherService;
import io.renren.modules.edu.vo.TEduTrainTeacherVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collector;
import java.util.stream.Collectors;

/**
 *
 * 培训管理-教师表
 * @author zwp zwp
 * @since 1.0.0 2022-07-11
 */
@Slf4j
@Service
public class TEduTrainTeacherServiceImpl extends ServiceImpl<TEduTrainTeacherDao, TEduTrainTeacherEntity> implements TEduTrainTeacherService {

    @Override
    public void save(TEduTrainTeacherDTO tEduTrainTeacherDTO) {
        TEduTrainTeacherEntity tEduTrainTeacherEntity = JacksonUtils.obj2pojo(tEduTrainTeacherDTO, TEduTrainTeacherEntity.class);
        this.baseMapper.insert(tEduTrainTeacherEntity);
    }

    @Override
    public void update(TEduTrainTeacherDTO tEduTrainTeacherDTO) {
        TEduTrainTeacherEntity tEduTrainTeacherEntity = JacksonUtils.obj2pojo(tEduTrainTeacherDTO, TEduTrainTeacherEntity.class);
        this.updateById(tEduTrainTeacherEntity);
    }

    @Override
    public Map<Long, List<TEduTrainTeacherVO>> queryTrainToTeacher(Long teacherInfoId) {
        log.info("获取该教师的报名情况");
        QueryWrapper<TEduTrainTeacherEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("edu_teacherinfo_id", teacherInfoId);
        List<TEduTrainTeacherEntity> tEduTrainTeacherEntityList = this.baseMapper.selectList(wrapper);
        if (CollectionUtils.isNotEmpty(tEduTrainTeacherEntityList)) {
            List<TEduTrainTeacherVO> tEduTrainTeacherVOList = JacksonUtils.listObj2pojo(tEduTrainTeacherEntityList, TEduTrainTeacherVO.class);
            return tEduTrainTeacherVOList.stream().collect(Collectors.groupingBy(e -> e.getEduTrainId()));
        }
        return null;
    }

    @Override
    public void confirmTrain(Long eduTrainId, Long eduTeacherInfoId) {
        log.info("参与报名");
        TEduTrainTeacherEntity tEduTrainTeacherEntity = new TEduTrainTeacherEntity();
        tEduTrainTeacherEntity.setEduTrainId(eduTrainId);
        tEduTrainTeacherEntity.setEduTeacherinfoId(eduTeacherInfoId);
        this.baseMapper.insert(tEduTrainTeacherEntity);
    }

    @Override
    public void deleteTrainTeaByTrainId(Long eduTrainId) {
        log.info("删除该活动下的教师情况");
        QueryWrapper<TEduTrainTeacherEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("edu_train_id", eduTrainId);
        this.baseMapper.delete(wrapper);
    }
}
